package Controller;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.Serializable;

import Communication.Client;


import Communication.MessageHandle;
import DB.SaleQuery;
import Entity.Sale;
import Entity.Worker;
import View.HandleNewSaleView;

/** This class controls and manages the process handle new sale and distribute customers per sales man */
public class HandleNewSaleController implements Serializable {
	private static final long serialVersionUID = 42L;
	/**  Required attributes */
	SaleQuery saleModel;
	HandleNewSaleView handleSaleView;
	Client client;
	Sale sle;
	String SaleID="0";
	Worker worker;

	/**
	 *  Constructor calling to listener class that attached to the process
	 * @param client
	 * @param workerMainMenuController
	 */
	public HandleNewSaleController(Client client, WorkerMainMenuController workerMainMenuController) {
		this.client = client;
		handleSaleView =  new HandleNewSaleView();
		handleSaleView.showWindow();			
		handleSaleView.addHandleNewSaleListeners(new HandleNewSaleListener(workerMainMenuController));

	}

	/**  Inner Listener class */ 
	class HandleNewSaleListener implements ActionListener
	{
		WorkerMainMenuController workerMainMenuController;
		SearchCustomerHistoryController SearchCustomerHistoryController;

		/**
		 * creats HandleNewSale Listener
		 * @param workerMainMenuController
		 */
		public HandleNewSaleListener(WorkerMainMenuController workerMainMenuController){
			this.workerMainMenuController = workerMainMenuController;
			saleModel = new SaleQuery();
			handleSaleView.showWindow();

		}


		/** Handle actions */
		public void actionPerformed(ActionEvent event) {


			if (event.getSource() == handleSaleView.getCustomersBtn())
			{
				SaleID=handleSaleView.getSaleRepId();
				getCustomers(SaleID);
				getCustomers(SaleID);
				if(saleModel.isEmpty(handleSaleView.getSaleRepId()))
					SystemController.displayMessage("SaleID Field Is Missing!");

				else{

					SearchCustomerHistoryController = new SearchCustomerHistoryController(
							client, HandleNewSaleController.this, handleSaleView.getSaleRepId());
					Sale Sale=new Sale();
					Sale.SetSaleID(handleSaleView.getSaleRepId());
					workerMainMenuController.hideWindow();



					try {

						/**  send to server according to complete the add process */
						MessageHandle msg = new MessageHandle(Sale, MessageHandle.HANDLE_SALE, false);
						msg.setMsg(SaleID);
						client.sendToServer(msg);


					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace( );
					}
					workerMainMenuController.hideWindow();
				}	
			}

			else if( event.getSource() == handleSaleView.getBackBtn())	
			{
				workerMainMenuController.showWindow();
				handleSaleView.hideWindow();
				handleSaleView.clearFields();	
			}
		}
	}
	/**
	 *  shows window
	 */
	public void showWindow() {
		handleSaleView.showWindow();		
	}
	/**
	 *  hides window
	 */
	public void hideWindow() {
		handleSaleView.hideWindow();		
	}

	/**
	 * handles the message received from server - if the DB was updated or not and why.
	 * @param msg
	 */
	public static void updataView(String msg){
		if (msg.compareTo("new")==0)
			SystemController.displayMessage("Customer List For Sale Has Been Sent");

		else SystemController.displayErrorMessage(" Failed: The Sale Not Exist");

	}

	/**
	 * this function gets the customers that already attached to this sale
	 * @param saleID
	 */
	private void getCustomers(String saleID) {
		worker = new Worker();
		MessageHandle msg = new MessageHandle(null, MessageHandle.GET_CUSTOMERS, false);
		String Msg= (client.getCurrentWorkerID()+"-"+saleID);
		msg.setMsg(Msg);
		try {
			client.sendToServer(msg);
		} catch (IOException e) {
			System.out.print("HandleSaleController->GetCustomers");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

